package modele;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;


public class ClassWriter 
{
	//lieu ou l'on veut que la classe soit générée
	private String destination;
	private ModeleLangage ml;
	private ModeleClasse  mc;
	
	
	public ClassWriter(String dest, ModeleLangage modeleLangage, ModeleClasse modeleClasse) 
	{
		this.destination = dest;
		this.ml = modeleLangage;
		this.mc = modeleClasse;
	}
	
	public void write() throws IOException
	{
	
		String lecture = "";
		int nbLigne = 0;
		
		File classe = new File(this.destination+"/"+mc.getNom()+"."+ml.getFormat());
		File modeleSquelette = new File(ml.getModeleFichier());
		
		//lecteur du modele
		BufferedReader bf = new BufferedReader(new FileReader(modeleSquelette));
		//ecrivain sur le fichier de classe
		BufferedWriter wr = new BufferedWriter(new FileWriter(classe));
		StringTokenizer parser;
		
		//on parcourt le fichier modele
		while( (lecture = bf.readLine()) != null)
		{
			String line = lecture;
			String tabLine [] = line.split(" ");
			for(int i =0;i<tabLine.length;i++)
			{				
				if(tabLine[i].equals("[name]"))
				{
					wr.write(mc.getNom());
				}
				else
				{
					wr.write(tabLine[i]);
				}
				wr.write(" ");
			}
			wr.write("\n");
		}
		wr.flush();
	}
}